package com.handu.luna.mall.controller;

import javax.transaction.Transactional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.handu.luna.core.domain.order.Cart;
import com.handu.luna.core.domain.request.RequestContext;
import com.handu.luna.core.domain.Context;
import com.handu.luna.core.domain.user.User;

@Transactional
public abstract class BaseController{
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	public static final String CURRENT_USER_OBJECT_ATTRIBUTE = "user";
	public static final String CURRENT_CART_OBJECT_ATTRIBUTE = "cart";
	public static final String MALL_OBJECT_ATTRIBUTE = "mall";
	public static final String IS_GUEST_ATTRIBUTE = "user";
    public static final String CONTEXT_PATH = "http://192.168.1.182:8999";
    
    public static final String EMAIL_CONTEXT_PATH = "http://192.168.1.182:9000";
    public static final String EMAIL_LOCAL_CONTEXT_PATH = "http://localhost:9000";
	
	User currentUser;
	boolean isGuest;
	Cart currentCart;
	RequestContext request;
	
	@Autowired
	Context context;
	
	void DEBUG(Object message){
		if(logger.isDebugEnabled()) logger.debug(message);
	}
	void DEBUG(Object message,Throwable t){
		if(logger.isDebugEnabled()) logger.debug(message, t);
	}	
	
	void INFO(Object message){
		logger.info(message);
	}
	void ERROR(Object message){
		logger.error(message);
	}	

	public User getCurrentUser() {
		return currentUser;
	}
	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}
	
	@Autowired
	private SessionUser session;

	public SessionUser getSession() {
		return session;
	}
	public void setSession(SessionUser session) {
		this.session = session;
	}
	
	public void init(){
		
	}	
	MallModelView buildModelView(){
		return buildModelView(null);	
	}
	
	MallModelView buildModelView(String viewName){
		System.err.println(viewName);
		MallModelView model = new MallModelView();
		model.setViewName(viewName);
		currentUser = new User();
		currentUser.setName(null);
		System.err.println(this.getSession().getUser()==null?currentUser:this.getSession().getUser());
		
		model.addObject(CURRENT_USER_OBJECT_ATTRIBUTE,this.getSession().getUser()==null?currentUser:this.getSession().getUser());
        model.add("contextPath",CONTEXT_PATH);
		/*model.addObject(CURRENT_CART_OBJECT_ATTRIBUTE, currentCart);
		model.addObject(IS_GUEST_ATTRIBUTE, isGuest);*/
		
		return model;	
	}
}
